package cn.yl.user.server.controller;


import cn.yl.api.server.result.Result;
import cn.yl.api.server.util.JwtUtil;
import cn.yl.api.server.util.WechatConfig;
import cn.yl.api.server.util.redis.RedisUtils;
import cn.yl.user.server.entity.User;
import cn.yl.user.server.entity.dto.UserDto;
import cn.yl.user.server.entity.vo.UserVo;
import cn.yl.user.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-04-22
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private WechatConfig wechatConfig;

    private final UserService userService;


    public UserController(UserService userService) {
        this.userService = userService;
    }

    private ThreadLocal<String> wechatJWT = new ThreadLocal<String>();


    @PostMapping("login")
    public Result login(@RequestBody UserVo userVo) {
        return userService.login(userVo);
    }

    @GetMapping("info")
    public Result info(HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        UserDto userDtoByJwt = userService.getUserDtoByJwt(jwt);
        return userDtoByJwt != null ? Result.ok().setData("item", userDtoByJwt) : Result.error().setMessage("登录信息过期，请重新登录");
    }

    @PutMapping("updateUser")
    public Result updateUser(@RequestBody UserVo userVo, HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        Boolean update = this.userService.updateByJwt(userVo, jwt);
        return update ? Result.ok() : Result.error();
    }

    @PutMapping("updatePassword")
    public Result updatePassword(@RequestBody UserVo userVo, HttpServletRequest request) {
        Boolean update = false;
        if (userVo.getPassword() != null && userVo.getPassword1() != null && userVo.getPassword2() != null && userVo.getPassword1().equals(userVo.getPassword2())) {
            String jwt = request.getHeader("jwt");
            update = this.userService.updateByJwt(userVo, jwt);
        }
        return update ? Result.ok() : Result.error();
    }


    @GetMapping("/login/wechat")
    public Result wxLogin(HttpServletResponse response) throws IOException {
        String uuId = UUID.randomUUID().toString().replace("-", "");
        System.out.println("state = " + uuId);
        return Result.ok().setData("state", uuId).setData("appid", wechatConfig.getAppid()).setData("redirect_uri", wechatConfig.getRedirectUri());
    }

    @GetMapping("wxlogin")
    public void getJwtByWechat(String access_token, String openid, String state) throws IOException {
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        System.out.println(Thread.currentThread().getName());
        String jwt = userService.loginByWechat(access_token, openid);
        redisUtils.putValue(state, jwt, 100000);
//        wechatJWT.set(userService.loginByWechat(access_token, openid)).;
    }

    @GetMapping("checkWxStatus")
    public Result checkWxStatus(String state) {
        Object o = redisUtils.getValue(state);
        System.out.println("state = " + state);
        redisUtils.delete(state);
        return Result.ok().setData("item", o.toString());
    }

//    @GetMapping("/t1")
//    public void test01(HttpServletResponse response) throws IOException {
//        response.setHeader("test", "test01");
//        response.sendRedirect("http://localhost:8080/#/");
//    }

}

